import Vue from 'vue'
import App from './App.vue'
import axios from 'axios'
import Login from './Login.vue'
import Home from './Home.vue'
import User from './components/User.vue'
import VueRouter from 'vue-router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import Copy from './components/copy.vue'
import Role from './components/Role.vue'
import store from './store'
import {initMenu} from './utils/utils'
Vue.use(ElementUI)
Vue.use(VueRouter)
axios.defaults.withCredentials=true
Vue.prototype.$ajax=axios;

const routes=[
  {
    path:'/',component:Login,meta:{
      auth:false
    },name:'登录'
  },
  {
    path:'/home',component:Home,children:[
      {
        path:'/user',component:User,meta:{
          auth:true
        }
      },
      {
        path:'/copy',component:Copy,meta:{
          auth:true
        }
      },{
        path:'/role',component:Role,meta:{
          auth:true
        }
      }
    ],meta:{
      auth:true
    }
  }
]

const router = new VueRouter({routes});
router.beforeEach((to,from,next)=>{
  if(to.path == '/'){
    next();
    return;
  }

  var username = store.state.user.username;
  if(username=='未登录'){
    if(to.meta.auth || !to.path){
      next({path:'/'});
      return
    }
  }else{
    initMenu(router, store);
    setTimeout(() => {
        next();
    }, 500);
  }
})

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
